package server;

import client.PaintAction;
import client.PaintingListener;
import client.PaintingModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Hp
 * Date: 25.05.11
 * Time: 21:32
 * To change this template use File | Settings | File Templates.
 */
public class ReadOnlyView extends JComponent implements PaintingListener, MouseMotionListener, MouseListener {
    private PaintingModel model;
    private PaintAction actionType;

    public PaintAction getActionType() {
        return actionType;
    }

    public void setActionType(PaintAction actionType) {
        this.actionType = actionType;
    }

    public ReadOnlyView()
    {
        super();
        addMouseMotionListener(this);
        addMouseListener(this);
        invalidate();
    }

    public PaintingModel getModel() {
        return model;
    }

    public void setModel(PaintingModel model) {
        if (this.model!=null)
        {
            model.removeListener(this);
        }
        this.model = model;
        if (model!=null)
        {
            model.addListener(this);
            setPreferredSize(new Dimension(model.getWidth(), model.getHeight()));
        }
    }

    public void onImageUpdate() {
        repaint();
    }

    public void repaint(long tm, int x, int y, int width, int height)
    {
        int x1 = x, y1 = y, x2 = x1 + width, y2 = y1 + height;
        x2 = Math.min(x2, model.getWidth());
        y2 = Math.min(y2, model.getHeight());
       // getGraphics().clipRect(x1,y1, x2-x1, y2-y1);
         if (model != null)
            model.draw((Graphics2D)getGraphics());
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {

    }

    public void mouseClicked(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {
    }

}
